// ./utils/getRef

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = {
    install: function install(Vue) {
        let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
        let directiveName = options.name || 'ref';
        Vue.directive(directiveName, {
            bind: function bind(el, binding, vnode) {
                // 自定义组件传入的值是函数 在这里执行传入组件实例
                // vnode.key是使用插件时起的名称
                binding.value(vnode.componentInstance || el, vnode.key);
            },
            update: function update(el, binding, vnode, oldVnode) {
                if (oldVnode.data && oldVnode.data.directives) {
                    let oldBinding = oldVnode.data.directives.find(function(directive) {
                        return directive.name === directiveName;
                    });
                    if (oldBinding && oldBinding.value !== binding.value) {
                        oldBinding && oldBinding.value(null, oldVnode.key);
                        // 如果指令绑定的值有变化，则更新组件实例
                        binding.value(vnode.componentInstance || el, vnode.key);
                        return;
                    }
                }
                // Should not have this situation
                if (vnode.componentInstance !== oldVnode.componentInstance || vnode.elm !== oldVnode.elm) {
                    binding.value(vnode.componentInstance || el, vnode.key);
                }
            },
            unbind: function unbind(el, binding, vnode) {
                binding.value(null, vnode.key);
            }
        });
    }
};
